//
//  YSVisitor.swift
//  新浪浪
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit


/*******************************************/
/*       注册按钮点击代理  */
/*****************设置代理协议和代理方法**********************************/

          //设置代理协议  ①①①①①①①①①
protocol visitorDelegate : NSObjectProtocol{

            //设置代理方法  ②②②②②②②②②             注意代理协议与代理,属性的位置
    func didRegister()
    func didLogin()
}
/**************设置代理协议和代理方法*************************/



class YSVisitor: UIView {
    
//            设置代理属性  ③③③③③③③③③
    weak var delegate: visitorDelegate?
    
    //自定义UI控件的三个步骤
    
    
    /*******************************************/
    /*      1⃣️ 重写   */
    /***************************************************/
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(white:237/255.0 , alpha: 1.0)
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
        //2⃣️
    /*******************************************/
    /*      私有方法    */
    /***************************************************/
    private func setUI (){
        
        startAnimation()
        //注意添加顺序!!!!!!!!!!!!!(遮罩)
        
        addSubview(homeIconImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        /*******************************************/
        /*      设置约束  start    */
        /***************************************************/
        
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(snp_centerX)
            make.width.equalTo(216)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.height.equalTo(30)
            make.width.equalTo(100)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    /*******************************************/
    /*      设置约束 end    */
    /***************************************************/
    
    
    
    /*******************************************/
    /*      动画  start  */
    /***************************************************/
    
    func startAnimation(){
    
        let animation = CABasicAnimation (keyPath: "transform.rotation")//transform.rotation
        animation.toValue = 2 * M_2_PI
        animation.duration = 10
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    /*******************************************/
    /*      对外调整view的方法    */
    /***************************************************/
    func setInfo(message:String,image:String,animation:Bool){
        
        messageLabel.text = message
        
        iconImageView.image = UIImage (named: image)
        if animation  {
            homeIconImageView.hidden = false
            startAnimation()
        }else{
            homeIconImageView.hidden = true
        }
    }
    
    /*******************************************/
    /*      代理方法    */
    /***************************************************/
    func login(){
        delegate?.didLogin()
//        print("denglu")
    }
    func register(){
        delegate?.didRegister()
//        print("zhuce")
    }
    
    
        //3⃣️
    /*******************************************/
    /*      懒加载 控件    */
    /***************************************************/
    
   private lazy var homeIconImageView : UIImageView = {
        let imageView = UIImageView()
        
        imageView.image = UIImage (named:"visitordiscover_feed_image_smallicon" )
        
        return imageView
        }()
   private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        
        imageView.image = UIImage (named:"visitordiscover_feed_image_house" )
        
        return imageView
        }()
   private lazy var messageLabel:UILabel = {
        let label = UILabel()
        
        label.backgroundColor = UIColor .darkGrayColor()
        label.numberOfLines = 0
        label.text = "既然琴瑟起,何以箫声默"
        label.textAlignment = .Center
        label.textColor = UIColor.whiteColor()
        
        return label
        }()
   private lazy var registerButton:UIButton = {
        let button = UIButton (type: UIButtonType.Custom)   /**********************??************/
        
        button.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.addTarget(self, action:"register" , forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
   private lazy var loginButton : UIButton = {
        let button = UIButton.init(type: UIButtonType.Custom)  /*******************??*****************/
        
        button.setBackgroundImage(UIImage (named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: .Normal)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
    lazy private var maskImageView:UIImageView = {           /*******************??*****************/
        let imageView = UIImageView()
        
        imageView.image = UIImage (named: "visitordiscover_feed_mask_smallicon")
        
        return imageView
        }()
    
    /*******************************************/
    /*      懒加载 控件 end   */
    /***************************************************/
    
}








